package test;

import java.util.*;

/**
 * 找出字符串中的第一字符
 * <p>
 * 1.写入map
 * 2.遍历，键值对放入另一map,下一次迭代判断map中是否有相同值，有返回key,无继续加入
 */
public class Test20210411question1 {
    public static void main(String[] args) {
        String a = "xingjiabao";
        char result = solution0411(a);
        System.out.println("第一次出现的字符为: " + result);
    }

    public static char solution0411(String message) {
//        List<StringBuilder> list = new ArrayList<StringBuilder>();
        Map<Integer, Character> map = new LinkedHashMap<Integer, Character>();
        Map<Integer, Character> mapTemp = new LinkedHashMap<Integer, Character>();
        char result = 0;
        char[] transferMessage = message.toCharArray();
        //所有数据存入map
        for (int i = 0; i < transferMessage.length; i++) {
            map.put(i + 1, transferMessage[i]);
        }
        Iterator iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            //遍历
            Integer key = (Integer) iterator.next();
            Character value = map.get(key);
            if (!mapTemp.containsValue(value)) {
                mapTemp.put(key, value);
            } else {
                result = value;
                break;
            }
        }
        return result;
    }
}
